package thread.multi;

public class Race implements Runnable {

    private static String winner;

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {

            if (Thread.currentThread().getName().equals("hare")) {
                try {
                    i = i+9;
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println(Thread.currentThread().getName()+"跑了"+i+"米。");

            boolean flag = gameover(i);
            if (flag) {
                break;
            }

        }
    }

    private boolean gameover(int distance) {
        boolean flag = false;
        if (winner!=null) {
            flag = true;
        } else if(distance >= 100) {
            winner = Thread.currentThread().getName();
            System.out.println("The winner is " + winner);
        }
        return flag;
    }

    public static void main(String[] args) {
        Race race = new Race();

        new Thread(race,"hare").start();
        new Thread(race,"tortoise").start();
    }
}
